package gameOfLife;

import interfaces.Cell;
import interfaces.World;

import lib.Location;



/**
 * 
 * The Dead Cell implementation in the Conway's Game of Life
 */
public class DeadCell implements Cell {
	LocationHelperImpl locHelper = new LocationHelperImpl();
	
	/**
	 * The transform rule for a alive Cell
	 */
	@Override
	public Cell transform(World w, Location loc) {		
		Location[] neighbors;
		if(w.is2D()){
			neighbors =  locHelper.getNeighbors(loc,w.getDimension(),w.getDimension());
		}else{
			neighbors =  locHelper.getNeighbors(loc,w.getDimension(),1);
		}
		int numAdjacent = 0;

		for(Location n : neighbors) {
			if(Math.abs(loc.getX() - n.getX()) <= 1 &&
					Math.abs(loc.getX() - n.getX()) <= 1){
				if(w.getCell(n) instanceof AliveCell)
					numAdjacent++;
			}
		}

		if(numAdjacent == 3)
			return new AliveCell();
		else
			return new DeadCell();
	
	}
	/**
	 * Get the name of this type of Cell
	 */
	public String getName(){
		return "DeadCell";
	}
	
}
